{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 利用Python进行数据分析 chapter6 数据加载/存储/文件格式\n",
    "\n",
    "> 2023/2/3\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### pandas读取文本数据\n",
    "\n",
    "read_csv  csv的默认分隔符是逗号\n",
    "\n",
    "read_table 默认分隔符是制表符\n",
    "\n",
    "指定分隔符用sep参数\n",
    "\n",
    "同时可以指定列名 names=['',..,'']\n",
    "\n",
    "header=None则会让pandas分配默认的列名\n",
    "\n",
    "使用index_col则可以指定列作为行索引\n",
    "\n",
    "skiprows=[1,2,4]参数则是跳过文件的1，2，4行\n",
    "\n",
    "na_values=['','',]用于接受一组表示缺失值的字符串\n",
    "\n",
    "nrows可以指定读取几行\n",
    "\n",
    "chunksize可以对文件进行逐块读取\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### pandas 将数据写入到文本文件\n",
    "\n",
    "`to_csv`方法可以将DF数据写到一个以`逗号`分割的文件中\n",
    "\n",
    "`sep`参数可以指定分隔符\n",
    "\n",
    "`na_rep`参数可以将NaN表示为指定的标记字符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### JSON数据"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> {\"name\": \"kaka\", \"age\": 35}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "obj = {\n",
    "    'name':'kaka',\n",
    "    'age':35\n",
    "} # type(obj)=dict 类型是字典\n",
    "\n",
    "asjson = json.dumps(obj)\n",
    "print(type(asjson),asjson)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
